package com.hanrui.android.net.presenter.message.myvote;

import android.content.Context;
import android.util.Log;

import com.hanrui.android.activity.LoginActivity;
import com.hanrui.android.library.base.BaseBean;
import com.hanrui.android.library.base.BasePresenter;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.model.MyVoteRecordBean;
import com.hanrui.android.library.net.ApiService;
import com.hanrui.android.library.net.RetrofitManager;
import com.hanrui.android.library.net.RxSchedulers;
import com.hanrui.android.net.contract.message.myvote.MyVoteRecordContract;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 *
 */

public class MyVoteRecordPresenter extends BasePresenter<MyVoteRecordContract.View> implements MyVoteRecordContract.Presenter {

    @Inject
    public MyVoteRecordPresenter(){}

    @Override
    public void recordView(final Context context,String mid) {

        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .recordView(mid)
                .compose(RxSchedulers.<BaseBean<MyVoteRecordBean>>applySchedulers())
                .compose(mView.<BaseBean<MyVoteRecordBean>>bindToLife())
                .subscribe(new Consumer<BaseBean<MyVoteRecordBean>>() {
                    @Override
                    public void accept(BaseBean<MyVoteRecordBean> dataResponse) throws Exception {
                        Log.e(Constant.tag,dataResponse.getCode()+"");
                        if (dataResponse.getCode() == 1){
                            mView.recordView(dataResponse.getData());
                        }else if (dataResponse.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else {
                            mView.showFaild(String.valueOf(dataResponse.getMessage()));
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild(throwable.getMessage());
                    }
                });

    }
}
